/**
 * create by young (young2f@outlook.com) on 2020/9/16
 */


'use strict';
import React from 'react';
import {Badge, Button, Form, Icon, InputNumber, notification, Select} from 'antd';

import {FormField, FormLayout, ModalFooter} from 'antd-extra';
import FormBase from '../../../components/Base/FormBase';
import {connect} from "dva";
import moment from 'moment';
import 'moment/locale/zh-cn';
import ViewTitle from "../../../components/ViewTitle";
import DescGroup from "../../../components/DescriptionGroup";

const _ = require('lodash');
const {Option} = Select;
moment.locale('zh-cn');
const table = 'ProcureMaterialReturnOrder';
@Form.create()
@connect(({
            user,
            global,
            material,
            procure,
            vendor
          }) => ({...user, ...procure, ...material, ...global, ...vendor}))
export default class ModalProcureReturnOrder extends FormBase {
  constructor(props) {
    super(props);
    const {values = {}} = this.props;
    this.state = {...values};
  }

  componentDidMount() {
    this.props.dispatch({
      type: 'global/getModel',
      payload: {table}
    });
  }


  renderFoot = () => {
    const {foot, value, modalRef, callback, record, form} = this.props;

    if (foot) {
      return foot(value, modalRef);
    } else {
      return (
        <ModalFooter>
          <Badge count={0} dot>
            <Icon style={{marginRight: '6px'}} type="notification" theme="twoTone" twoToneColor="#eb2f96"/>
          </Badge>
          <Badge count={0} dot>
            <div
              style={{marginRight: '12px'}}>{'点击确认后，系统将自动扣除【物料库存】数量'}</div>
          </Badge>
          <Button type="primary" onClick={() => {
            form.validateFields((err, values) => {
              if (err) {
                const {code} = err;
                const {errors} = code;
                errors.forEach(item => {
                  notification.error(item);
                });
                return;
              }
              // const {returnCount} = values;
              // const {orderCode, code, name} = record;
              _.merge(record, values);
              callback(record);
              modalRef.close();
            });
          }}>确定</Button>
          <Button onClick={() => {

            modalRef.close();
          }}>取消</Button>
        </ModalFooter>
      );
    }
  };


  render() {
    let {form, footDisabled, style, record = {}} = this.props;
    const model = this.props[`${table}Model`];
    if (!model) return;
    const {returnCount = 0} = record;
    const _labels = _.filter(model, (item, key) => {
      if (key !== 'labels' && _.includes(item.enableArea, 'detail')) {
        item.key = key;
        return item;
      }
    });

    _.map(_labels, item => {
      const {_type = "", key} = item;
      if (_type.toLowerCase() === 'switch') {
        record[key] = record[key] ? "是" : "否";
      }
      if (_type.toLowerCase() === 'date') {
        record[key] = record[key] ? moment(record[key]).format('YYYY-MM-DD') : '';
      }
    });
    return (
      <div style={{...style}}>


        <ViewTitle title={`退货物料信息`}
                   level={1}
        ></ViewTitle>
        <DescGroup
          {...{
            labels: _labels,
            values: record,
            size: 'default'
          }}
        ></DescGroup>
        {<FormLayout form={form} cols={2}>
          <FormField label={'退货出库数量'}
                     name={'returnCount'}
                     required={true}
                     initialValue={returnCount}
          >
            <InputNumber disabled></InputNumber>
          </FormField>
        </FormLayout>}
        {this.renderFoot()}
      </div>
    );
  }
}

